//
//  SNAssociatorViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/16.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNAssociatorViewController.h"
#import "SNAssociatorCell.h"
@interface SNAssociatorViewController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** ZXUserDetailInfo * userDetailInfo */
@property (nonatomic,strong) ZXUserDetailInfo * userDetailInfo;
@end

@implementation SNAssociatorViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"详细信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.

    
    CGRect frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    self.tableView = tableView;
    [self setupInit:tableView];
    
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
    
    [[ZXHTTPManager sharedManager] getUserInfoWithUserId:self.USERID certNo:nil completed:^(NSDictionary *jsonDict, NSError *error) {
        
        logError;
        
        if([jsonDict[@"status"] integerValue] == 1){
            // 失败
            NSLog(@"%@",jsonDict[@"message"]);
            [SNProgressManager showToView:self.view message:jsonDict[@"message"] afterDelay:2];
            return ;
        };
        NSLog(@"%@",jsonDict);
        NSDictionary * dict = jsonDict[@"result"][0];
        ZXUserDetailInfo * userDetailInfo = [ZXUserDetailInfo mj_objectWithKeyValues:dict];
        
        self.userDetailInfo = userDetailInfo;
        
        [self.tableView reloadData];
        
    }];
    
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}

- (void)setupInit:(UITableView *)tableView {
    tableView.delegate = self;
    tableView.dataSource = self;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 10)];
    headerView.backgroundColor = HexRGB(0xf5f5f5);
    tableView.tableHeaderView = headerView;
    tableView.backgroundColor = HexRGB(0xf5f5f5);
    //    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.separatorInset = UIEdgeInsetsZero;
    tableView.tableFooterView = [UIView new];
    [self setHeights:tableView];
    [self registerNib:tableView];
    [self registerClass:tableView];
}
#pragma mark - <Register>
/** registerNib */
- (void)registerNib:(UITableView *)tableView {
        [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SNAssociatorCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SNAssociatorCell class])];
}
/** registerClass */
- (void)registerClass:(UITableView *)tableView {
    //    [tableView registerClass:[SNTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SNTableViewCell class])];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    //    [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([UITableViewHeaderFooterView class])];
}
#pragma mark - <SetHeights>
/** setHeights */
- (void)setHeights:(UITableView *)tableView {
    
    tableView.estimatedRowHeight = 47;
    tableView.rowHeight = UITableViewAutomaticDimension;

}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SNAssociatorCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNAssociatorCell class])];
    
    [cell configWithUserDetailInfo:self.userDetailInfo indexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
#pragma mark - <UITableViewDelegate>

/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
//    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/** 取消 选中 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%s", __func__);
}

@end
